<?php
/**
 * Created by PhpStorm.
 * User: lee
 *
 * 功能：退款处理后需要进行的异步任务
 * 版本：v1.0
 *
 *
 * 规则：5分钟拉取一次待处理数据
 *
 *
 *
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\service\trade\OrderRefund as sOrderRefund;
use Biz\module\Classes\OrderRefund as clsOrderRefund;
use Medoo\Medoo;

if (isset($argv[1])) {
    $stopMiunte = $argv[1];
} else {
    $stopMiunte = date('Y-m-d H:i:00', time());
}

$startMiunte = date('Y-m-d H:i:00', strtotime($stopMiunte) - 60 * 5);


$where = [
    'AND' => [
        'status'       => mOrderRefund::STATUS_COMPLETE,
        'edittime[<>]' => [$startMiunte, $stopMiunte],
    ],
    'GROUP'=>'order_no'
];;


try {
    //取出待处理的数据
    $refundList = mOrderRefund::select('*', $where);

    if (empty($refundList)) {
        throw new \Exception("当前时刻没有待处理的数据");
    }

    foreach ($refundList as $refund) {

        try {

            (new clsOrderRefund())->repairOrderByOrderNo($refund['order_no']);

        } catch (\Exception $e) {

            echo sprintf("%s    order_no:%s    异常：%s  \n", date('Y-m-d H:i:s'), $refund['order_no'],  json_encode([
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine(),
                'e_msg'  => $e->getMessage()
            ], JSON_UNESCAPED_UNICODE));

        } finally {
            echo sprintf("%s    order_no:%s    sql日志：%s   \n", date('Y-m-d H:i:s'), $refund['order_no'], mOrderRefund::sqlLogs());

            mOrderRefund::clearLogs();
        }
    }
} catch (\Exception $e) {

    echo sprintf("%s    %s\n", date('Y-m-d H:i:s'), $e->getMessage());

}






